#include<iostream>
#include<vector>
#include<ranges>
#include<algorithm>
#include<unordered_map>
// 编程语言
// C++20
// 代码长度
// 1.11KB
// 用时
// 791ms
// 内存
// 2.01MB
using namespace std;

struct edge{
	int x,y,t;
};

namespace wang{

}
vector<edge> edges;
struct UnionSet{
	vector<int> ump;
	int reserved_bangs;
	UnionSet()=default;
	UnionSet(int from,int to_included){
		ump.resize(to_included+1);
		reserved_bangs =to_included-from+1;
		for(;from <=to_included;++from){
			ump[from]=from;
		}	
	}
	
	int operator()(int n){
		int sup =ump[n];
		if(ump[sup]!=sup){return ump[n]=ump[sup]=operator()(sup);}
		else{return sup;}
	}
	void connect(int a,int b){
		ump[operator()(a)]= ump[operator()(b)];
		--reserved_bangs;
	}
};


int N,M;
int main(){
	cin>>N>>M;
	for(int i=0;i<M;++i){
		int x,y,t;
		cin>>x>>y>>t;
		edges.push_back({x,y,t});
	}
	UnionSet us(1,N);
	ranges::sort(edges,[](const edge& ed_a,const edge& ed_b){return ed_a.t<ed_b.t;});
	int time=0;
	for(auto[x,y,t]:edges){
		if(us(x)!=us(y)){
			us.connect(x,y);
			time =max(time,t);
			if(us.reserved_bangs ==1){break;}
		}
	}
	cout<<(us.reserved_bangs==1 ? time:-1);
	return 0;
}